<?php
/**
 * usermonitorpagedefine.php
 * Create on 2015-05-28
 * Create by yuebin
 */
namespace haibao\cms\data\behavior;


class UserMonitorPageDefine extends \haibao\cms\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\behavior\UserMonitorPageDefine');
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
	}
	
	public function searchRegionCode($code){
		$data = array();
		$sql = 'select * from UserMonitorRegionDefine where Code=%s';
		$result = $this->query($sql,array($code));
		
		while ($row = $result->fetch_assoc()){
			$model = new \haibao\cms\model\data\behavior\UserMonitorRegionDefine();
			$model->loadArray($row);
			array_push($data,$model);
		}
		
		return $data;
	}
	
	public function getRegionNames($pageId){
		$data = array();
		$sql = 'select * from UserMonitorRegionDefine where PageId=%s';
		$result = $this->query($sql,array($pageId));
		
		while ($row = $result->fetch_assoc()){
			$model = new \haibao\cms\model\data\behavior\UserMonitorRegionDefine();
			$model->loadArray($row);
			array_push($data,$model);
		}
		
		return $data;
	}
	
	public function add($model){
		parent::add($model);
		if ($model->Regions){
			foreach ($model->Regions as $region){
				$region->PageId = $model->Id;
			}
			
			parent::addAll($model->Regions);
		}
	}
	
	public function updateById($id,$model){
		if (isset($model->Regions)){
			$sql = 'delete from UserMonitorRegionDefine where PageId = '.$this->escape($model->Id);
			$this->query($sql);
			
			foreach ($model->Regions as $region){
				if ($region->PageId){
					parent::add($region);
				}
			}
		}
	
		parent::updateById($id,$model);
	}
	
}